package com.onLineCar.manages.service.impl;

import com.onLineCar.manages.pojo.China;
import com.onLineCar.manages.mapper.BaseMapper;
import com.onLineCar.manages.service.BaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhucaiyi
 * @since 2023-12-10
 */
@Service
@Slf4j
public class BaseServiceImpl extends ServiceImpl<BaseMapper, China> implements BaseService {

    /**
     * 重写父类方法，返回中国的城市树列表
     *
     * @return 城市树列表
     */
    @Override
    public List<China> cityTree() {
        List<China> list = this.list(null);
        return list.stream()
                .filter(first -> ObjectUtils.isNotEmpty(first.getPid()) && first.getPid().equals(NumberUtils.INTEGER_ZERO))
                .map(second -> second.setChildren(setChildren(second.getId(), list)))
                .collect(Collectors.toList());
    }


    private List<China> setChildren(Integer pid, List<China> chinaList) {
        return chinaList.stream()
                .filter(first -> ObjectUtils.isNotEmpty(first.getPid()) && first.getPid().equals(pid))
                .map(second -> second.setChildren(setChildren(second.getId(), chinaList)))
                .collect(Collectors.toList());
    }


}
